<template>
  <div class="xtx-goods-page">
    <div class="container">

      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem
          v-if="goods"
          :to="`/category/${goods.categories[1].id}`"
          >{{ goods.categories[1].name }}</XtxBreadItem
        >
        <XtxBreadItem
          v-if="goods"
          :to="`/category/sub/${goods.categories[0].id}`"
          >{{ goods.categories[0].name }}</XtxBreadItem
        >
        <XtxBreadItem v-if="goods">{{ goods.name }}</XtxBreadItem>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info" v-if="goods">
        <div class="media">
          <GoodsImage :images="goods.mainPictures"></GoodsImage>
          <GoodsSales></GoodsSales>
        </div>
        <div class="spec">
          <GoodName :goods="goods"></GoodName>
          <!-- spu -->
          <GoodsSku :goods="goods" skuId="1369155862131642369" @change="sku"></GoodsSku>
          <!-- 数量选择组件 -->
          <XtxNumbox v-model="count" label="数量" :max="goods.inventory"></XtxNumbox>
          <!-- 按钮组件 -->
          <XtxButton type="primary" style="margin-top:20px;" @click="insertCart">加入购物车</XtxButton>
        </div>
      </div>

      <!-- 商品推荐 -->
      <GoodsRelevant v-if="goods" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>
          <!-- 注意事项 -->
          <GoodsWarn></GoodsWarn>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/produce'
import GoodsRelevant from './components/goods-relevant'
import { nextTick, provide, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'

export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodName, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const goods = useGoods()
    // 依赖注入
    provide('goods', goods)

    const sku = (data) => {
      if (data.skuId) {
        goods.value.price = data.price
        goods.value.oldPrice = data.oldPrice
        goods.value.inventory = data.inventory
      }
      currSku.value = data // 记录选择后的sku  可能有、可能无
    }

    // 选择数量组件传值的承载
    const count = ref(1)

    const store = useStore()
    const currSku = ref(null)
    const insertCart = () => {
    // 约定假如购物车字段
      if (currSku.value && currSku.value.skuId) {
        const { skuId, specsText: attrsText, inventory: stock } = currSku.value
        const { name, price, id, mainPictures } = goods.value
        store.dispatch('cart/insertCart', {
          skuId,
          attrsText,
          stock,
          id,
          name,
          price,
          nowPrice: price,
          picture: mainPictures[0],
          selected: true,
          count: count.value,
          isEffective: true
        }).then(() => {
          Message({ text: '商品添加成功！' })
        })
        // id skuId name attrsName picture price nowPrice selected stock count isEffective
      } else {
        Message({ type: 'error', text: '请选择完整规格' })
      }
    }

    return { goods, sku, count, insertCart }
  }
}

// 获取商品详情 进行渲染
const useGoods = () => {
  const goods = ref(null)
  const route = useRoute()
  // 当商品ID发生改变
  watch(
    () => route.params.id,
    (newVal) => {
      if (newVal && newVal === route.params.id) {
        findGoods(route.params.id).then((data) => {
          // 当更新商品的时候，可以让其他的组件第一时间触发
          // 让商品数据为空 然后使用v-if 的组可以重新销毁和渲染
          goods.value = null
          nextTick(() => {
            goods.value = data.result
          })
        })
      }
    },
    { immediate: true }
  )

  // 返回
  return goods
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
</style>
